package com.person.sys.action;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import com.person.commons.struts2.BaseAction;
import com.person.commons.utils.Constants;
import com.person.sys.model.SysFace;
import com.person.sys.service.SysFaceService;

public class SysFaceAction extends BaseAction {
	private SysFace face;
	private SysFaceService sysFaceService;
	private String faceId;
	private String ieTitle;
	private String statusText;
	private String bannerText;
	private String bannerFont;
	
	private String logoWidth;
	private String logoHight;
	private String bmpTopWidth;
	private String bmpTopHight;
	
	private String bmpLoginName;
	private String bmpLoginNameFileName;
	
	private String logoName;
	private String logoNameFileName;
	
	private String bmpTopName;
	private String bmpTopNameFileName;
	
	private String bmpDesk;
	private String bmpDeskFileName;
	
	private String imageContextType;
	private String imageType;
	private String rootDir;
	private String photoDir;
	private String datestr;//时间戳，用于上传文件名
	

	/**
	 * 显示单位管理界面 如果已存在单位，就显示单位信息，否则的话显示添加界面
	 */
	public String index() {

		face = sysFaceService.search();
		if (face != null) {// 存在记录，将记录返回
			bmpTopName=face.getBmpTopName();
			setBmpTopNameFileName(request.getRealPath(File.separator) + Constants.FACE_PHOTO_DIR+bmpTopName);
			logoName=face.getLogoName();
			setLogoNameFileName(request.getRealPath(File.separator) + Constants.FACE_PHOTO_DIR+logoName);
			bmpLoginName=face.getBmpLoginName();
			setBmpLoginNameFileName(request.getRealPath(File.separator) + Constants.FACE_PHOTO_DIR+bmpLoginName);
			setFace(face);
		}
		return "input";
	}

	/**
	 * 修改界面
	 */
	public String modifyface()throws Exception {
		if(getBmpTopName()!=null)
			face.setBmpTopName(uploadImage(getBmpTopName(),getBmpTopNameFileName()));//上传顶部图片，并将设置图片名
		if(getLogoName()!=null)
			face.setLogoName(uploadImage(getLogoName(),getLogoNameFileName()));//上传LOGO，并将设置图片名
		if(getBmpLoginName()!=null)
			face.setBmpLoginName(uploadImage(getBmpLoginName(),getBmpLoginNameFileName()));//上传登录图片，并将设置图片名
		if(getBmpDesk()!=null)
			face.setBmpDeskName(uploadImage(getBmpDesk(),getBmpDeskFileName()));//上传桌面图片，并将设置图片名
		info = sysFaceService.Modify(face);
		return "success";
	}

	/**
	 * 上传图片
	 * @param image
	 * @param imgFileName
	 * @return
	 */
	
	public String uploadImage(String image,String imgFileName) {
		if (image != null && !image.equals("")) {
			//获得图片类型
			imageType = imgFileName.substring(
					imgFileName.lastIndexOf("."));
			// 创建用于存放图片的物理路径
			rootDir = request.getRealPath(File.separator) + Constants.FACE_PHOTO_DIR;
			Date datenow=new Date();
			datestr=String.valueOf(datenow.getTime());
			photoDir = rootDir +datestr+imageType;

			File rootFile = new File(rootDir);
			File photoFile = new File(photoDir);
			// 创建目录
			if (!rootFile.exists()) {
				rootFile.mkdirs();
			}
			//TODO 没有反馈给用户是否有重名的文件，默认的处理是直接删除同名文件
			if (photoFile.exists()) {				
				photoFile.delete();
			}
			try {
				FileOutputStream fos = new FileOutputStream(photoDir);
				FileInputStream fis = new FileInputStream(image);
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = fis.read(buffer)) > 0) {
					fos.write(buffer);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return datestr+imageType;
	}

	public SysFace getFace() {
		return this.face;
	}

	public void setFace(SysFace face) {
		this.face = face;
	}

	public String getFaceId() {
		return this.faceId;
	}

	public void setFaceId(String faceId) {
		this.faceId = faceId;
	}

	public String getIeTitle() {
		return this.ieTitle;
	}

	public void setIeTitle(String ieTitle) {
		this.ieTitle = ieTitle;
	}

	public String getStatusText() {
		return this.statusText;
	}

	public void setStatusText(String statusText) {
		this.statusText = statusText;
	}

	public String getBannerText() {
		return this.bannerText;
	}

	public void setBannerText(String bannerText) {
		this.bannerText = bannerText;
	}

	public String getBannerFont() {
		return this.bannerFont;
	}

	public void setBannerFont(String bannerFont) {
		this.bannerFont = bannerFont;
	}

	public String getLogoName() {
		return this.logoName;
	}

	public void setLogoName(String logoName) {
		this.logoName = logoName;
	}

	public String getLogoWidth() {
		return this.logoWidth;
	}

	public void setLogoWidth(String logoWidth) {
		this.logoWidth = logoWidth;
	}

	public String getLogoHight() {
		return this.logoHight;
	}

	public void setLogoHight(String logoHight) {
		this.logoHight = logoHight;
	}

	

	public String getBmpTopWidth() {
		return this.bmpTopWidth;
	}

	public void setBmpTopWidth(String bmpTopWidth) {
		this.bmpTopWidth = bmpTopWidth;
	}

	public String getBmpTopHight() {
		return this.bmpTopHight;
	}

	public void setBmpTopHight(String bmpTopHight) {
		this.bmpTopHight = bmpTopHight;
	}

	public String getBmpLoginName() {
		return this.bmpLoginName;
	}

	public void setBmpLoginName(String bmpLoginName) {
		this.bmpLoginName = bmpLoginName;
	}

	public void setSysFaceService(SysFaceService sysFaceService) {
		this.sysFaceService = sysFaceService;
	}


	public String getRootDir() {
		return rootDir;
	}

	public void setRootDir(String rootDir) {
		this.rootDir = rootDir;
	}

	public String getPhotoDir() {
		return photoDir;
	}

	public void setPhotoDir(String photoDir) {
		this.photoDir = photoDir;
	}


	public String getImageContextType() {
		return imageContextType;
	}

	public void setImageContextType(String imageContextType) {
		this.imageContextType = imageContextType;
	}


	public String getBmpLoginNameFileName() {
		return bmpLoginNameFileName;
	}

	public void setBmpLoginNameFileName(String bmpLoginNameFileName) {
		this.bmpLoginNameFileName = bmpLoginNameFileName;
	}

	public String getLogoNameFileName() {
		return logoNameFileName;
	}

	public void setLogoNameFileName(String logoNameFileName) {
		this.logoNameFileName = logoNameFileName;
	}

	public String getBmpTopName() {
		return bmpTopName;
	}

	public void setBmpTopName(String bmpTopName) {
		this.bmpTopName = bmpTopName;
	}

	public String getBmpTopNameFileName() {
		return bmpTopNameFileName;
	}

	public void setBmpTopNameFileName(String bmpTopNameFileName) {
		this.bmpTopNameFileName = bmpTopNameFileName;
	}

	public String getBmpDesk() {
		return bmpDesk;
	}

	public void setBmpDesk(String bmpDesk) {
		this.bmpDesk = bmpDesk;
	}

	public String getBmpDeskFileName() {
		return bmpDeskFileName;
	}

	public void setBmpDeskFileName(String bmpDeskFileName) {
		this.bmpDeskFileName = bmpDeskFileName;
	}

	public SysFaceService getSysFaceService() {
		return sysFaceService;
	}

}
